home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / dlists / fonts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.1 KB  |  303 lines

  1. /* Copyright 1996, Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16.  
  17. /* fonts.c - create display lists for an outline and a filled font 
  18.  *    and use glCallLists() to display a string with them
  19.  * 
  20.  *      Left Mouse Button        - change incidence and azimuth angles
  21.  *      Middle Mousebutton        - change the twist angle based on
  22.  *                      horizontal mouse movement
  23.  *      Right Mousebutton        - zoom in and out based on vertical
  24.  *                      mouse movement
  25.  *    <f> key                - cycle thru outline/filled/stroke fonts
  26.  *      <R> Key                - reset viewpoint
  27.  *    Escape key            - exit the program
  28.  */
  29. #include <GL/gl.h>
  30. #include <GL/glu.h>
  31. #include <GL/glut.h>
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <math.h>
  36.  
  37. #include "fonts.h"    /* contains descriptions for 3 sample fonts */
  38.  
  39. /*  Function Prototypes  */
  40.  
  41. GLvoid  initgfx( GLvoid );
  42. GLvoid  drawScene( GLvoid );
  43. GLvoid  reshape( GLsizei, GLsizei );
  44. GLvoid  keyboard( GLubyte, GLint, GLint );
  45. GLvoid  mouse( GLint, GLint, GLint, GLint );
  46. GLvoid  motion( GLint, GLint );
  47.  
  48. GLuint  create3DFont( GLfloat font[][1+MAX_STROKES*3] );
  49. GLvoid  cycleFont( GLvoid );
  50.  
  51. void  resetView( GLvoid );
  52. void  polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  53. void  printHelp( char * );
  54.  
  55. /* Global Definitions */
  56.  
  57. #define KEY_ESC    27    /* ascii value for the escape key */
  58.  
  59. /* Global Variables */
  60.  
  61. static GLint        action;
  62. static enum         actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  63.  
  64. static GLint        xStart = 0, yStart = 0;
  65. static GLfloat         distance, twistAngle, incAngle, azimAngle;
  66.  
  67. static GLuint outlineBase, filledBase, strokeBase, currentBase;
  68. static char string[] = "OpenGL Programming 1";
  69.  
  70. void 
  71. main( int argc, char *argv[] )
  72. {
  73.     GLsizei width, height;
  74.  
  75.     glutInit( &argc, argv );
  76.  
  77.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  78.     height = glutGet( GLUT_SCREEN_HEIGHT );
  79.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  80.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  81.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  82.     glutCreateWindow( argv[0] );
  83.  
  84.     initgfx();
  85.  
  86.     glutMouseFunc( mouse );
  87.     glutMotionFunc( motion );
  88.     glutKeyboardFunc( keyboard );
  89.     glutReshapeFunc( reshape );
  90.     glutDisplayFunc( drawScene ); 
  91.  
  92.     printHelp( argv[0] );
  93.  
  94.     outlineBase = create3DFont( outlineFont );
  95.     filledBase = create3DFont( filledFont );
  96.     strokeBase = create3DFont( strokeFont );
  97.  
  98.     currentBase = filledBase;
  99.  
  100.     glutMainLoop();
  101. }
  102.  
  103. GLvoid
  104. printHelp( char *progname )
  105. {
  106.     fprintf(stdout, "\n%s - render a string using fonts stored "
  107.         "in display lists.\n"
  108.         "Left Mousebutton    - move eye position\n"
  109.         "Middle Mousebutton    - change twist angle\n"
  110.         "Right Mousebutton    - move up/down to zoom in/out\n"
  111.         "<f> key        - cycle through fonts\n"
  112.         "<R> Key        - reset viewpoint\n"
  113.         "Escape key        - exit the program\n\n",
  114.         progname);
  115. }
  116.  
  117. GLvoid
  118. resetView( GLvoid )
  119. {
  120.     distance = 250.0;
  121.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  122.     incAngle = 0.0;
  123.     azimAngle = 0.0;
  124. }
  125.  
  126. GLvoid 
  127. initgfx( void )
  128. {
  129.     glClearColor( 0.0, 0.0, 0.0, 0.0 );
  130.  
  131.     resetView();
  132. }
  133.  
  134. /* Create a display list for each letter in the font description
  135.  * passed in.
  136.  */
  137. GLuint 
  138. create3DFont( GLfloat font[][1+MAX_STROKES*3] )
  139. {
  140.     GLint mode, i, j;
  141.     GLuint fontBase;
  142.     GLfloat fontScale;
  143.  
  144.     fontBase = glGenLists( 256 );
  145.     for (i = 0; font[i][0] != END_OF_LIST; i++) {
  146.         glNewList( fontBase+(GLuint)font[i][0], GL_COMPILE );
  147.         for (j = 1; mode = font[i][j]; j += 3) {
  148.             if (mode == FONT_BEGIN) {
  149.                 fontScale = font[i][j+2];
  150.                 glBegin( (GLint) font[i][j+1] );
  151.             } else if (mode == FONT_NEXT) {
  152.                 glVertex2f( font[i][j+1]*fontScale, 
  153.                         font[i][j+2]*fontScale );
  154.             } else if (mode == FONT_END) {
  155.                 glVertex2f( font[i][j+1]*fontScale, 
  156.                         font[i][j+2]*fontScale );
  157.                 glEnd();
  158.             } else if (mode == FONT_ADVANCE) {
  159.                 glTranslatef( font[i][j+1]*fontScale, 
  160.                           font[i][j+2]*fontScale, 0.0 );
  161.             }
  162.         }
  163.         glEndList();
  164.     }
  165.     return fontBase;
  166. }
  167.  
  168. GLvoid 
  169. reshape( GLsizei width,  GLsizei height )
  170. {
  171.     GLdouble        aspect;
  172.  
  173.     glViewport(0, 0, (GLint)width, (GLint)height);
  174.  
  175.     glMatrixMode(GL_PROJECTION);
  176.     glLoadIdentity();
  177.     aspect = (GLdouble) width / (GLdouble) height;
  178.     gluPerspective( 45.0, aspect, 10.0, 500.0 );
  179.     glMatrixMode(GL_MODELVIEW);
  180. }
  181.  
  182. void
  183. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  184.             GLfloat twist)
  185. {
  186.     glTranslatef( 0.0, 0.0, -distance);
  187.     glRotatef( -twist, 0.0, 0.0, 1.0);
  188.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  189.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  190. }
  191.  
  192. GLvoid 
  193. keyboard( GLubyte key, GLint x, GLint y )
  194. {
  195.     switch (key) {
  196.     case 'f':    /* toggle filled mode */
  197.         cycleFont();
  198.         glutPostRedisplay();
  199.         break;
  200.     case 'R':
  201.         resetView();
  202.         glutPostRedisplay();
  203.         break;
  204.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  205.         exit(0);
  206.     }
  207. }
  208.  
  209. GLvoid 
  210. mouse( GLint button, GLint state, GLint x, GLint y )
  211. {
  212.     static GLint buttons_down = 0;
  213.  
  214.     if (state == GLUT_DOWN) {
  215.         switch (button) {
  216.         case GLUT_LEFT_BUTTON:
  217.             action = MOVE_EYE;
  218.             break;
  219.         case GLUT_MIDDLE_BUTTON:
  220.             action = TWIST_EYE;
  221.             break;
  222.         case GLUT_RIGHT_BUTTON:
  223.             action = ZOOM;
  224.             break;
  225.         }
  226.  
  227.         /* Update the saved mouse position */
  228.         xStart = x;
  229.         yStart = y;
  230.     } else {
  231.         if (--buttons_down == 0) 
  232.             action = MOVE_NONE;
  233.     }
  234.  
  235. }
  236.  
  237. GLvoid
  238. motion( GLint x, GLint y )
  239. {
  240.     switch (action) {
  241.     case MOVE_EYE:
  242.         /* Adjust the eye position based on the mouse position */
  243.         azimAngle += (GLdouble) (x - xStart);
  244.         incAngle -= (GLdouble) (y - yStart);
  245.         break;
  246.     case TWIST_EYE:
  247.         /* Adjust the eye twist based on the mouse position */
  248.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  249.         break;
  250.     case ZOOM:
  251.         /* Adjust the eye distance based on the mouse position */
  252.         distance -= (GLdouble) (y - yStart)/10.0;
  253.         break;
  254.     default:
  255.         printf("unknown action %d\n", action);
  256.     }
  257.     
  258.     /* Update the stored mouse position for later use */
  259.     xStart = x;
  260.     yStart = y;
  261.  
  262.     glutPostRedisplay();
  263. }
  264.  
  265. /* Define user input functions */
  266. GLvoid  
  267. cycleFont( GLvoid )
  268. {
  269.     if ( currentBase == outlineBase ) {
  270.         currentBase = filledBase;
  271.     } else if ( currentBase == filledBase ) {
  272.         currentBase = strokeBase;
  273.     } else {
  274.         currentBase = outlineBase;
  275.     }
  276. }
  277.  
  278. GLvoid 
  279. drawScene( GLvoid )
  280. {
  281.  
  282.     glClear( GL_COLOR_BUFFER_BIT );
  283.  
  284.     glColor3f( 1.0, 1.0, 1.0 );
  285.     glPushMatrix();
  286.         polarView( distance, azimAngle, incAngle, twistAngle );
  287.  
  288.         glPushMatrix();
  289.             glTranslatef( -110.0, 0.0, 0.0 );
  290.             /* save the current ListBase value, set it to 
  291.              * the start of the font and use the string itself
  292.              * to index into the display lists
  293.              */
  294.             glPushAttrib(GL_LIST_BIT);
  295.                 glListBase( currentBase );
  296.                 glCallLists( strlen(string), GL_UNSIGNED_BYTE, 
  297.                         (unsigned char *) string );
  298.             glPopAttrib();
  299.         glPopMatrix();
  300.     glPopMatrix();
  301.     glutSwapBuffers();
  302. }
  303.